﻿using Dapper;
using socialnetworkApp.Domain.IRepository;
using socialnetworkApp.Domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace socialnetworkApp.Domain.Manager
{
    public class UsersManager
    {
        private readonly IUserRepository _userRepository;
        public UsersManager(IUserRepository userRepository)
        {

            _userRepository = userRepository;
        }

        #region 个人信息管理


    

        public async Task<int> UpdateNickname(Users users) {

            string sql = "UPDATE `users`  SET `nickname` = @nickname  WHERE `id` = @id;";

           return (int)await _userRepository.Update(users, sql);
          
        }


        public async Task<int> UpdateEmail(Users users)
        {

            string sql = "UPDATE `users`  SET `email` = @email  WHERE `id` = @id;";

            return (int)await _userRepository.Update(users, sql);

        }
        public async Task<int> UpdateSignature(Users users)
        {

            string sql = "UPDATE `users`  SET `signature` = @signature  WHERE `id` = @id;";

            return (int)await _userRepository.Update(users, sql);

        }

        public async Task<int> UpdateSex(Users users)
        {

            string sql = "UPDATE `users`  SET `sex` = @sex  WHERE `id` = @id;";

            return (int)await _userRepository.Update(users, sql);

        }

        public async Task<int> UpdateUserRoutesAndButtonsAndRoles(Users users)
        {

            string sql = "UPDATE users SET routes = @routes ,buttons=@buttons,roles=@roles  WHERE id = @id;";

            return (int)await _userRepository.Update(users, sql);

        }




        public async Task<Users> UserLogin(Users users)
        {
            string sql = "select * from users WHERE users.username =@username and users.`password`= @password;";
           return  await   _userRepository.SelectFirst(users,sql);
        }


        public async Task<Users> GetUserInfo(Users users) {

            string sql = "select u.username,u.headeImg, u.email,u.headeImg,u.id,u.sex,u.nickname,u.signature ,u.routes,u.roles,u.buttons from users u WHERE u.id=@id;";
            return await _userRepository.SelectFirst(users,sql);
        }


        public async Task<Users> IsExistUser(Users users) {

            string sql = "SELECT u.id,u.email FROM `users` u where  u.email=@email;";
            return await _userRepository.SelectFirst(users,sql);
        }


        public async Task<long> AddUser(Users users) {

            string sql = "INSERT  into  users(id,nickname,email,`password`,username) VALUE(@id,@nickname,@email,@password,@username);";
            return await _userRepository.Insert(users,sql);
        }
        public async Task<Users> GetUserById(string uId)
        {

            string sql = "SELECT * from users u WHERE u.id =@id;";
                   Users user = new Users {Id=uId };
                   var userinfo = await _userRepository.SelectFirst(user, sql);


            return userinfo;

        }

        /// <summary>
        /// 根据用户ids 查询用户信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<List<Users>> GetUserListById(List<Users> ids) {

            string sql = "SELECT * from users u WHERE u.id =@id;";
            List<Users> users = new List<Users>();
            foreach (var item in ids)
            {
            var user=   await _userRepository.SelectFirst(item, sql);

                users.Add(user);
            }  
            return users;

        }

        #endregion

    }
}
